<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :is-root="true" @add-depts="showAddDeptsDialog" />
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <tree-tools :node-data="data" @del-depts="getDepartmentsList" @add-depts="showAddDeptsDialog" @edit-depts="showEditDeptsDialog" />
          </template>
        </el-tree>
      </el-card>
      <add-depts ref="addDepts" :show-dialog.sync="showDialog" :departs-list="departsList" :node-data="nodeData" @add-depts-success="getDepartmentsList" @closeDialogFn="closeDialog" />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDepts from './components/add-depts.vue'
import { getDepartmentsApi } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'

export default {
  name: 'Departments',
  components: {
    treeTools,
    addDepts
  },
  data() {
    return {
      showDialog: false,
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      company: {
        name: '强盛集团',
        manage: '高启强',
        id: ''
      },
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      nodeData: {},
      departsList: [],
      loading: false
    }
  },
  created() {
    this.getDepartmentsList()
  },
  methods: {
    async getDepartmentsList() {
      // 请求回来之前打开loading效果
      this.loading = true
      const { data } = await getDepartmentsApi()
      this.departsList = data.depts
      this.departs = transListToTreeData(data.depts, '')
      // 请求回来之后关闭loading效果
      this.loading = false
    },
    closeDialog() {
      this.showDialog = false
    },
    showAddDeptsDialog(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$refs.addDepts.showTitle = '新增部门'
      this.$refs.addDepts.getEmployeeSimple()
    },
    showEditDeptsDialog(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$refs.addDepts.showTitle = '修改部门'
      // 使用nextTick才可以等页面加载完获取id
      this.$nextTick(() => {
        this.$refs.addDepts.getDepartDetail()
      })
    }
    // 常规写法
    // transListToTreeData(list, searchVal) {
    //   // 思路：先找父级，然后根据父级id去列表的所有数据中找与之对应的pid
    //   const arr = []
    //   // 找到所有父级
    //   list.forEach(item => {
    //     if (item.pid === searchVal) {
    //       arr.push(item)
    //     }
    //   })
    //   // 根据父级的id和列表中所有数据的pid进行对比
    //   arr.forEach(item => {
    //     const children = list.filter(obj => obj.pid === item.id) || []
    //     item.children = children
    //   })
    //   return arr
    // }
    // 递归写法
    // transListToTreeData(list, searchVal) {
    //   // 思路：先找父级，然后根据父级id去列表的所有数据中找与之对应的pid
    //   const arr = []
    //   list.forEach(item => {
    //     if (item.pid === searchVal) {
    //       // 找到父亲了
    //       const children = this.transListToTreeData(list, item.id) || []
    //       item.children = children
    //       arr.push(item)
    //     }
    //   })
    //   return arr
    // }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
